﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Urb.Weather.Core.MeasureUnit;

namespace Urb.Weather.Core.Util
{
    public static class TemperatureHelper
    {
        public static decimal ConvertTemperature(decimal fromTemperature, TemperatureUnit fromUnit, TemperatureUnit toUnit)
        {
            if (fromUnit == toUnit)
                return fromTemperature;

            switch (fromUnit)
            {
                case TemperatureUnit.Fahrenheit:
                    switch(toUnit)
                    {
                        case TemperatureUnit.Celsius:
                            return 5m * (fromTemperature - 32m) / 9m;
                        default:
                            throw new InvalidOperationException();
                    }
                case TemperatureUnit.Celsius:
                    switch (toUnit)
                    {
                        case TemperatureUnit.Fahrenheit:
                            return 32m + (9m * fromTemperature / 5m);
                        default:
                            throw new InvalidOperationException();
                    }
                default:
                    throw new InvalidOperationException();
            }
        }

        public static decimal ConvertTemperatureTo(this TemperatureUnit toUnit, decimal fromTemperature, TemperatureUnit fromUnit)
        {
            return ConvertTemperature(fromTemperature, fromUnit, toUnit);
        }
    }
}
